<template>
    <div class="pageContainer">
        <el-container class="bg-white rounded-lg">
            <el-header class="flex items-center justify-between" style="border-bottom: 1px solid #eeeeee;">
                <div>
                    <el-button type="primary" @click="onAdd">新增</el-button>
                    <el-popconfirm title="确定批量删除该配置吗？" @confirm="onMultiDelete">
                        <template #reference>
                            <el-button type="primary">批量删除</el-button>
                        </template>
                    </el-popconfirm>
                </div>
                <el-button type="primary" text @click="onSearch">
                    <el-icon :size="20">
                        <Refresh />
                    </el-icon>
                </el-button>
            </el-header>
            <el-container>
                <div class="w-full">
                    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName"
                        @selection-change="onSelectionChange">
                        <el-table-column type="selection" width="40" />
                        <el-table-column label="序号" width="90" align="center" fixed="left" prop="rank">
                            <template #default="{ $index, row }">
                                {{ (currentPage - 1) * limit + $index + 1 }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="规格名称" min-width="150" />
                        <el-table-column prop="defaults" label="规格值" min-width="200" />
                        <el-table-column prop="order" label="排序" min-width="100" />
                        <el-table-column prop="status" label="状态" min-width="150">
                            <template #default="{ $index, row }">
                                <el-switch v-model="row.status" :active-value="1" :inactive-value="0" size="small"
                                    @change="onStatusChange(row)" />
                            </template>
                        </el-table-column>
                        <el-table-column label="修改时间" width="200">
                            <template #default="{ row }">
                                {{ dayjs(row.update_time).format('YYYY-MM-DD HH:mm:ss') }}
                            </template>
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" min-width="200" align="center">
                            <template #default="scope">
                                <el-button link type="primary" @click="onEdit(scope.row)">修改</el-button>
                                <el-popconfirm title="确定删除该配置吗？" @confirm="onDelete(scope.row)">
                                    <template #reference>
                                        <el-button type="primary" link>删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div style="height: 50px;" class="flex items-center justify-center">
                        <el-pagination layout="prev, pager, next" background :current-page="currentPage"
                            :total="totalCount" :page-size="10" @current-change="onPageChange" />
                    </div>
                </div>
            </el-container>
        </el-container>
        <FormDrawer ref="drawerRef" :title="drawerTitle" @submit="onSubmit">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100">
                <el-form-item label="规格名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入规格名称" clearable />
                </el-form-item>
                <el-form-item label="排序" prop="order">
                    <el-input-number v-model="form.order" :min="1" :max="1000" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
                <el-form-item label="规格值" prop="defaults">
                    <TagInput v-model="form.defaults"/>
                    <!-- <el-input v-model="form.default"/> -->
                </el-form-item>
            </el-form>
        </FormDrawer>
    </div>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { getSkusList, addSkus, updateSkusById, deleteSkusById, multiDeleteSkusByIds, upateSkusStatusById } from '../../api/skus'
import FormDrawer from '../../components/FormDrawer.vue'
import  TagInput from '../../components/TagInput.vue'
import { useTableSearch, useForm } from '../../composables/common'

// 查询相关
// 定义查询表单
const searchform = reactive({
    keyword: "",
})

// 定义清除查询条件的表单
const clearSearchform = () => {
    searchform.keyword = ''
}

const {
    tableRowClassName,
    onSearch,
    getData,
    onClearSearch,
    onPageChange,
    currentPage,
    tableData,
    totalCount,
    limit
} = useTableSearch({
    searchform,
    getTableList: getSkusList,
    clearSearchform
})

// do not use same name with ref
const form = reactive({
    id: 0,
    name: '',
    defaults: '',
    order: 50,
    status: 1,
    create_time: '2022-06-11 15:32:10',
    update_time: '2022-06-11 15:32:10'
})

const rules = {
    name: [{ required: true, message: '规格名称不能为空', trigger: 'blur' }],
    defaults: [{ required: true, message: '规格描述不能为空', trigger: 'blur' }],
    order: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
}

const {
    formRef,
    drawerRef,
    drawerTitle,
    onAdd,
    onEdit,
    onDelete,
    onSubmit,
    onStatusChange,
    onSelectionChange,
    onMultiDelete
} = useForm({
    title: '规格', // 功能名称
    form, // form表单，必须
    tableData, // 表单数据来源
    getData, // 更新表单数据接口
    add: addSkus, // 新增接口
    update: updateSkusById, // 修改接口
    delete: deleteSkusById, // 删除接口
    updateStatus: upateSkusStatusById, // 修改状态
    multiDelte: multiDeleteSkusByIds // 批量删除
})

</script>
